package com.ewzj.bigdata.mvp.presenter

import android.app.Application
import android.preference.PreferenceManager
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.utils.RxUtils
import com.ewzj.bigdata.mvp.contract.CollectActivityContract
import com.ewzj.bigdata.mvp.model.reqbean.ReqNormalCollect
import com.ewzj.bigdata.mvp.model.reqbean.ReqcollectBean
import com.ewzj.bigdata.mvp.model.rspbean.BaseData
import com.ewzj.bigdata.mvp.model.rspbean.BaseListData
import com.ewzj.bigdata.mvp.model.rspbean.CollectionBean
import com.ewzj.bigdata.mvp.ui.adapter.CollectionAdapter
import com.google.gson.Gson
import com.jess.arms.mvp.BasePresenter
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import me.jessyan.rxerrorhandler.core.RxErrorHandler
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class CollectActivityPresenter @Inject constructor(val mGson: Gson, val mErrorHandler: RxErrorHandler, val application: Application, view: CollectActivityContract.View, model: CollectActivityContract.Model) : BasePresenter<CollectActivityContract.Model, CollectActivityContract.View>(model, view) {
    val datas = arrayListOf<CollectionBean>()
    val adapter = CollectionAdapter(datas)
    var page=1
    val context=mRootView as AppCompatActivity
    fun loadScanRecord() {
        page=1
        val reqcollectBean = ReqcollectBean()
        reqcollectBean.setP(1)
        reqcollectBean.page_num = AppKey.PAGE_NUM
        mModel.CollectListdata(reqcollectBean).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(object :ErrorHandleSubscriber<BaseData<BaseListData<CollectionBean>>>(mErrorHandler){
                    override fun onNext(t: BaseData<BaseListData<CollectionBean>>) {
                        page++
                        datas.clear()
                        datas.addAll(t.data!!.list)
                        adapter.setNewData(datas)
                        if (t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.disableLoadMoreIfNotFullPage()
                        }
                    }

                })
    }
    fun loadMoreData(){
        val reqcollectBean = ReqcollectBean()
        reqcollectBean.setP(page)
        reqcollectBean.page_num = AppKey.PAGE_NUM
        mModel.CollectListdata(reqcollectBean).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doAfterTerminate { if (adapter.isLoading){
                    adapter.loadMoreComplete()
                } }
                .subscribe({t ->
                    run{
                        adapter.addData(t.data!!.list)
                        if (t.data!!.list.size<AppKey.PAGE_NUM){
                            adapter.loadMoreEnd()
                        }
                    }

                },{adapter.loadMoreFail()})
    }
    fun initAdapter(recyclerView: RecyclerView) {
        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter=adapter
        recyclerView.addItemDecoration(HorizontalDividerItemDecoration.Builder(context).colorResId(R.color.color_divider)
                .size(2).margin(5, 5).build())
        adapter.setOnLoadMoreListener(object : BaseQuickAdapter.RequestLoadMoreListener {
            override fun onLoadMoreRequested() {
                loadMoreData()
            }

        }, recyclerView)
        adapter.cancelListener = object : CollectionAdapter.CancelListener {
            override fun cancelsub(collectionBean: CollectionBean) {
                datas.remove(collectionBean)
                Observable.just(collectionBean).map { t ->val reqNormalCollect = ReqNormalCollect()
                run{
                    reqNormalCollect.list_id=t.getId().toString()
                    reqNormalCollect.table_id=t.getTable_id().toString()
                }
                reqNormalCollect
                }.flatMap { t ->  mModel.cancelCollect(t) }
                        .compose(RxUtils.applySchedulers(mRootView))
                        .subscribe(object :ErrorHandleSubscriber<BaseData<Any>>(mErrorHandler){
                            override fun onNext(t: BaseData<Any>?) {

                            }

                        })
                adapter.notifyDataSetChanged()
                recyclerView.adapter = adapter
            }
        }

    }

}